﻿<template>
  <div></div>
</template>

<script>
// ws事件相关的操作部分
import WSTrigger from '@/components/WebSocketComponents/WebSocketConsole.vue'
import { commonUse_defaultMethod, commonUse_changeOutputParams } from '@/components/commonUse.js'
import axios from 'axios'
export default {
  components: { WSTrigger },
  data () {
    return {
      loading: false,
      loadingText: '页面加载中...',
      // 获取的设定的页面信息
      pageFormInfo: '',
      // 页面表单的基本信息
      models: {},
      rules: {},
      // 页面中所有的单个元素
      allSingleElement: [],
      // 当前事件的信息
      currentEventInfo: {
        no: '',
        name: '',
        category: '',
        request_parameters: '',
        response_data: ''
      },
      // 当为展示流程信息时
      currentShow: 'basicInfo',
      // 页面初始化的判断
      changeModels: false,
      // 判断事件的类型，RequestUrl是以~开头的是本地事件，否则是http事件
      isLocalEvent: true,
      // 当为流程界面时，判断界面流程相关的按钮的状态
      workflowBtnStatus: {
        Delete: [],
        Save_add: [],
        Save_edit: [],
        Submit: [],
        Pass: [],
        Reject: [],
        SelectReject: [],
        Stop: [],
        Apply: [],
        SubmitPending: [],
        ActivePending: []
      },
      // 保存事件后得到的primaryKey的主键/status等参数
      workFlowParams: {
        primaryKey: '',
        status: ''
      },
      // 事件的请求和响应的数据----api的键/页面展示的键/值/事件编号
      // inputArray----apiKey/pageKey/eventNo
      // outputArray----apiKey/pageKey/value/eventNo
      pagePrimaryObj: {
        inputArray: [],
        outputArray: []
      },
      // 当前执行事件的集合
      currentEventsArray: [],
      // 对于特殊的需要选人的节点
      NextUserList: null,
      currentClickType: '',
      showUserDialog: false,
      // 需要调用ws的弹窗相关的信息
      wsDialogInfo: {
        visible: false,
        title: '',
        wsApiRouter: ''
      }
    }
  },
  methods: {
    // 根据事件编号获取该事件的key值(事件编号、执行的顺序)
    getEventKeyByEventNo (targetEventNo, evalIndex) {
      let allDictionary = null
      if (this.currentEventsArray[evalIndex - 1].eventsNo && this.currentEventsArray[evalIndex - 1].eventsNo === targetEventNo) {
        allDictionary = this.currentEventsArray[evalIndex - 1]
      }
      // 每次将this.pagePrimaryObj.inputArray清空
      this.pagePrimaryObj.inputArray = []
      if (allDictionary && !this.$isEmptyObject(allDictionary) && allDictionary.inputDic) {
        // 输入的参数格式为：api中的键名 = 页面展示的键名
        for (let i = 0; i < allDictionary.inputDic.length; i++) {
          if (allDictionary.inputDic[i].inputKey.indexOf('=') !== -1) {
            let dicInputArray = allDictionary.inputDic[i].inputKey.split('=')
            let obj = {
              apiKey: dicInputArray[0],
              pageKey: dicInputArray[1],
              eventNo: targetEventNo
            }
            this.pagePrimaryObj.inputArray.push(obj)
          }
        }
      }
      // 获取输出的参数
      if (allDictionary && !this.$isEmptyObject(allDictionary) && allDictionary.outputDic) {
        // 输出的参数格式为： 页面展示的键名 = api中的键名
        for (let i = 0; i < allDictionary.outputDic.length; i++) {
          if (allDictionary.outputDic[i].outputKey.indexOf('=') !== -1) {
            let dicOutputArray = allDictionary.outputDic[i].outputKey.split('=')
            let hadPushed = false
            // 判断该事件的输出参数是否已经加入过一次
            if (this.pagePrimaryObj.outputArray.length) {
              for (let i = 0; i < this.pagePrimaryObj.outputArray.length; i++) {
                if (targetEventNo === this.pagePrimaryObj.outputArray[i].eventNo && this.pagePrimaryObj.outputArray[i].apiKey === dicOutputArray[1] && this.pagePrimaryObj.outputArray[i].pageKey === dicOutputArray[0]) {
                  hadPushed = true
                }
              }
            }
            if (!hadPushed) {
              let obj = {
                apiKey: dicOutputArray[1],
                pageKey: dicOutputArray[0],
                value: null,
                eventNo: targetEventNo
              }
              this.pagePrimaryObj.outputArray.push(obj)
            }
          }
        }
      }
    },
    // 获取表单的基本信息
    getFormInfo (genList) {
      for (let i = 0; i < genList.length; i++) {
        if (genList[i].type === 'grid') {
          genList[i].columns.forEach(item => {
            this.getFormInfo(item.list)
          })
        } else if (genList[i].type === 'tabs') {
          for (let t = 0; t < genList[i].tabList.length; t++) {
            this.getFormInfo(genList[i].tabList[t].list)
          }
        } else {
          // 控制表单是否可以输入-----在已办列表中点击查看详细
          if (this.dialogType === 'showDetail' && genList[i].options && Object.keys(genList[i].options).indexOf('disabled') >= 0) {
            genList[i].options.disabled = true
          }
          // 获取页面中所有的单个元素---表单元素
          this.allSingleElement.push(genList[i])
          // 将每一个的model属性当做唯一的标识---赋予初始值
          if (genList[i].type === 'table') {
            this.models[genList[i].model] = genList[i].tableData
          } else if (genList[i].type === 'editTable') {
            this.models[genList[i].model] = genList[i].tableData
          } else if (genList[i].type === 'Switch' || genList[i].type === 'switch') {
            this.models[genList[i].model] = genList[i].options.defaultValue ? genList[i].options.defaultValue : false
          } else {
            let defaultValue = genList[i].options.defaultValue
            let fixedValue = {
              formKey: this.dynamicUid,
              limit: 10,
              page: 1
            }
            let rowData = (this.clickRow && !this.$isEmptyObject(this.clickRow)) ? JSON.parse(JSON.stringify(this.clickRow)) : null
            let pPageRowData = (this.pPageRow && !this.$isEmptyObject(this.pPageRow)) ? JSON.parse(JSON.stringify(this.pPageRow)) : null
            let getDefaultValue = defaultValue ? commonUse_defaultMethod(defaultValue, null, fixedValue, rowData, pPageRowData) : null
            this.models[genList[i].model] = getDefaultValue
          }
          // 获取表单验证的规则
          if (this.rules[genList[i].model]) {
            this.rules[genList[i].model] = [...this.rules[genList[i].model], ...genList[i].rules.map(item => {
              if (item.pattern) {
                return {...item, pattern: item.pattern}
              } else {
                return {...item}
              }
            })]
          } else {
            this.rules[genList[i].model] = [...genList[i].rules.map(item => {
              if (item.pattern) {
                return {...item, pattern: new RegExp(item.pattern)}
              } else {
                return {...item}
              }
            })]
          }
        }
      }
      // 编辑时，需要初始化弹窗
      if ((this.dialogType === 'editTable' || this.dialogType === 'selfDefineDialogTable' || this.dialogType === 'showDetail') && !this.$isEmptyObject(this.models)) {
        let rowData = null
        if (this.pPageRow && !this.$isEmptyObject(this.pPageRow)) {
          rowData = JSON.parse(JSON.stringify(this.pPageRow))
        }
        if (this.clickRow && !this.$isEmptyObject(this.clickRow)) {
          rowData = rowData ? Object.assign(rowData, JSON.parse(JSON.stringify(this.clickRow))) : JSON.parse(JSON.stringify(this.clickRow))
        }
        for (let modelKey in this.models) {
          if (rowData && !this.$isEmptyObject(rowData)) {
            for (let rowKey in rowData) {
              if (modelKey === rowKey) {
                this.models[modelKey] = rowData[rowKey]
              } else {
                this.$set(this.models, rowKey, rowData[rowKey])
              }
            }
          }
        }
      }
    },
    // 获取表单的数据(提交表单时)
    getFormData (childItem) {
      if (this.$refs['formMenuLayout'].validateFormMethod() && childItem && childItem.eventsArray && childItem.eventsArray.length) {
        this.loading = true
        this.currentEventsArray = JSON.parse(JSON.stringify(childItem.eventsArray))
        this.getEventsInfoByEventNo(this.models, this.currentEventsArray[0].eventsNo, null, 1, null)
      }
    },
    // 按钮执行事件
    triggerSelfdefineEvent (childItem) {
      this.loading = true
      this.currentEventsArray = JSON.parse(JSON.stringify(childItem.eventsArray))
      let isInit = null
      if (this.currentEventsArray[0].eventsNo && this.currentEventsArray[0].eventsType === 'triggerEventType') {
        isInit = 'triggerEventType'
      }
      this.getEventsInfoByEventNo(this.models, this.currentEventsArray[0].eventsNo, isInit, 1)
    },
    // 根据事件编号获取事件详细---models数据、事件编号、是否是初始化事件、多个事件一次触发时当前事件的index、事件的类别（delete/save/submit/pass/reject/stop）
    getEventsInfoByEventNo (submitData, eventItemNo, isInit, workflowEventIndex, workflowEventItem) {
      // 获取该事件的key值
      this.getEventKeyByEventNo(eventItemNo, workflowEventIndex)
      // 获取事件的详细
      this.$get('/events4dform/' + eventItemNo).then(data => {
        let dataInfo = data.data.result
        if (dataInfo.code === 'failed') {
          this.loading = false
          this.$message({
            message: dataInfo.msg,
            type: 'error'
          })
        }
        if (dataInfo.code === 'success') {
          this.currentEventInfo = {
            no: dataInfo.data.no,
            name: dataInfo.data.name,
            category: dataInfo.data.category,
            request_parameters: dataInfo.data.request_parameters,
            response_data: dataInfo.data.response_data
          }
          // 需要先判断事件是否为ws事件
          if (this.currentEventInfo.request_parameters) {
            let parametersObj = JSON.parse(this.currentEventInfo.request_parameters)
            if (parametersObj.RequestUrl.indexOf('~') === 0 && parametersObj.RequestMethod === 'WS') {
              // 获取触发事件的parameters
              let evalParamters = {}
              // 将this.clickRow和submitData合并
              let submitData = JSON.parse(JSON.stringify(this.models))
              if (this.pPageRow && !this.$isEmptyObject(this.pPageRow)) {
                submitData = Object.assign(JSON.parse(JSON.stringify(this.pPageRow)), submitData)
              }
              if (this.clickRow && !this.$isEmptyObject(this.clickRow)) {
                submitData = Object.assign(JSON.parse(JSON.stringify(this.clickRow)), submitData)
              }
              if (this.pagePrimaryObj.inputArray.length) {
                this.pagePrimaryObj.inputArray.forEach(item => {
                  // 找到该事件对应的参数
                  if (item.eventNo === eventItemNo) {
                    // 判断apiKey是否是 Query.key的结构（Query表示的是字段名，key表示的是该对象中的一个属性）
                    if (item.apiKey.indexOf('.') !== -1) {
                      let apiObjString = item.apiKey.split('.')
                      // 需要先判断该对象是否已经存在于evalParamters中
                      if (Object.keys(evalParamters).indexOf(apiObjString[0]) === -1) {
                        evalParamters[apiObjString[0]] = {}
                      }
                      evalParamters[apiObjString[0]][apiObjString[1]] = this.getInputParamsMethods(item.pageKey, submitData)
                    } else {
                      evalParamters[item.apiKey] = this.getInputParamsMethods(item.pageKey, submitData)
                    }
                  }
                })
              }
              // 修改后的参数--如果有词典表，则获取词典表中的数据值，否者就直接为models中的值
              let parameters = this.$isEmptyObject(evalParamters) ? submitData : evalParamters
              let wsQueryStringArray = []
              if (parametersObj.RequestQueryString && !this.$isEmptyObject(parametersObj.RequestQueryString) && parameters && !this.$isEmptyObject(parameters)) {
                for (let searchKey in parametersObj.RequestQueryString) {
                  for (let parametersKey in parameters) {
                    if (searchKey === parametersKey) {
                      let queryArrayString = searchKey + '=' + parameters[parametersKey]
                      wsQueryStringArray.push(queryArrayString)
                    }
                  }
                }
              }
              // 获取ws请求的url地址
              let wsApiRouter = parametersObj.RequestUrl.slice(1)
              if (wsQueryStringArray.length) {
                wsApiRouter = wsApiRouter + '?' + wsQueryStringArray.join('&')
              }
              // 触发ws相关的事件
              this.$set(this.wsDialogInfo, 'visible', true)
              this.$set(this.wsDialogInfo, 'title', dataInfo.data.name)
              this.$set(this.wsDialogInfo, 'wsApiRouter', wsApiRouter)
              this.loading = false
            } else {
              if (parametersObj.RequestUrl.indexOf('~') === 0) {
                this.isLocalEvent = true
              } else {
                this.isLocalEvent = false
              }
              // 触发事件前修改事件参数
              this.changeEventsInfoBeforeSubmit(submitData, eventItemNo, isInit, workflowEventIndex, workflowEventItem)
            }
          } else {
            this.$message({
              message: '没有获取到触发事件的相关信息',
              type: 'error'
            })
            this.loading = false
          }
        }
      }).catch(error => {
        this.loading = false
        this.$message({
          message: error,
          type: 'warning'
        })
      })
    },
    // 修改输入参数的公共方法
    getInputParamsMethods (pageKey, submitData) {
      let inputKeyValue = ''
      let fixedValue = {
        formKey: this.dynamicUid,
        primaryKey: this.workFlowParams.primaryKey,
        status: this.workFlowParams.status,
        limit: 10,
        page: 1
      }
      let rowData = (this.clickRow && !this.$isEmptyObject(this.clickRow)) ? JSON.parse(JSON.stringify(this.clickRow)) : null
      let pPageRowData = (this.pPageRow && !this.$isEmptyObject(this.pPageRow)) ? JSON.parse(JSON.stringify(this.pPageRow)) : null
      inputKeyValue = pageKey ? commonUse_defaultMethod(pageKey, submitData, fixedValue, rowData, pPageRowData) : null
      return inputKeyValue
    },
    // 获取页面配置时定义的所有的参数this.pagePrimaryObj.inputArray中的键值对应的参数
    changeEventsInfoBeforeSubmit (submitData, eventItemNo, isInit, workflowEventIndex, workflowEventItem) {
      if (this.pPageRow && !this.$isEmptyObject(this.pPageRow)) {
        submitData = Object.assign(JSON.parse(JSON.stringify(this.pPageRow)), submitData)
      }
      if (this.clickRow && !this.$isEmptyObject(this.clickRow)) {
        submitData = Object.assign(JSON.parse(JSON.stringify(this.clickRow)), submitData)
      }
      // 获取触发事件的parameters
      let evalParamters = {}
      if (this.pagePrimaryObj.inputArray.length) {
        this.pagePrimaryObj.inputArray.forEach(item => {
          // 找到该事件对应的参数
          if (item.eventNo === eventItemNo) {
            // 判断apiKey是否是 Query.key的结构（Query表示的是字段名，key表示的是该对象中的一个属性）
            if (item.apiKey.indexOf('.') !== -1) {
              let apiObjString = item.apiKey.split('.')
              // 需要先判断该对象是否已经存在于evalParamters中
              if (Object.keys(evalParamters).indexOf(apiObjString[0]) === -1) {
                evalParamters[apiObjString[0]] = {}
              }
              evalParamters[apiObjString[0]][apiObjString[1]] = this.getInputParamsMethods(item.pageKey, submitData)
            } else {
              evalParamters[item.apiKey] = this.getInputParamsMethods(item.pageKey, submitData)
            }
          }
        })
      }
      let toEvalMethodJsonString = this.$isEmptyObject(evalParamters) ? '' : evalParamters
      this.triggerRowEventMethod(submitData, eventItemNo, isInit, workflowEventIndex, workflowEventItem, toEvalMethodJsonString)
    },
    // 修改触发事件后，输出参数对应的词典表信息
    changeOutputParames (data, eventItemNo) {
      let fixedValue = {
        formKey: this.dynamicUid,
        primaryKey: this.workFlowParams.primaryKey,
        status: this.workFlowParams.status,
        limit: 10,
        page: 1
      }
      let changeParamsArray = commonUse_changeOutputParams(data, eventItemNo, this.pagePrimaryObj.outputArray, this.models, fixedValue)
      if (changeParamsArray && changeParamsArray.length) {
        changeParamsArray.forEach(item => {
          this.$set(this.pagePrimaryObj.outputArray[item.index], 'value', item.pageValue)
          if (item.pageKey === '$_PrimaryKey') {
            this.workFlowParams.primaryKey = item.pageValue
          } else {
            this.$set(this.models, item.pageKey, item.pageValue)
          }
        })
      }
    },
    // 触发事件的方法
    triggerRowEventMethod (submitData, eventItemNo, isInit, workflowEventIndex, workflowEventItem, parameters) {
      if (this.isLocalEvent) {
        // 如果是本地事件的触发方式
        this.$post('/events4dform/' + eventItemNo + '/eval', {
          this_data: btoa(encodeURIComponent(JSON.stringify(submitData))),
          parameters: parameters
        }).then(data => {
          let dataInfo = data.data.result
          this.afterEval(dataInfo.code, dataInfo.data, isInit, eventItemNo, workflowEventIndex, workflowEventItem, dataInfo.msg)
        }).catch(error => {
          this.loading = false
          this.$message({
            message: error,
            type: 'error'
          })
        })
      } else {
        // http事件的出发方式
        if (this.currentEventInfo.request_parameters) {
          let requestObj = JSON.parse(this.currentEventInfo.request_parameters)
          if (!this.$isEmptyObject(requestObj) && requestObj.RequestUrl && requestObj.RequestMethod) {
            let axiosUrl = requestObj.RequestUrl
            // 改变path
            if (requestObj.RequestPath && !this.$isEmptyObject(requestObj.RequestPath)) {
              // 获取请求的url中的path---将url中的变量的改为获取到的值
              for (let pathKey in requestObj.RequestPath) {
                for (let modelKey in submitData) {
                  if (pathKey === modelKey) {
                    axiosUrl = axiosUrl.replace('{' + pathKey + '}', submitData[pathKey])
                  }
                }
              }
            }
            // 修改query的值
            let axiosQuery = {}
            if (requestObj.RequestQueryString && !this.$isEmptyObject(requestObj.RequestQueryString) && parameters) {
              for (let searchKey in requestObj.RequestQueryString) {
                for (let parametersKey in parameters) {
                  if (searchKey === parametersKey) {
                    axiosQuery[searchKey] = parameters[parametersKey]
                  }
                }
              }
            }
            // 修改postData的值
            let axiosPostData = {}
            if (requestObj.RequestPostData && !this.$isEmptyObject(requestObj.RequestPostData) && parameters) {
              for (let postKey in requestObj.RequestPostData) {
                for (let parametersKey in parameters) {
                  if (postKey === parametersKey) {
                    axiosPostData[postKey] = parameters[parametersKey]
                  }
                }
              }
            }
            axios({
              url: axiosUrl,
              method: requestObj.RequestMethod,
              // headers: requestObj.RequestHeader,
              data: axiosPostData,
              params: axiosQuery
            }).then(data => {
              let dataInfo = data.data.result
              this.afterEval(dataInfo.code, dataInfo.data, isInit, eventItemNo, workflowEventIndex, workflowEventItem, dataInfo.msg)
            }).catch(error => {
              this.loading = false
              this.$message({
                message: error,
                type: 'error'
              })
            })
          }
        }
      }
    },
    // 获取流程表单的按钮显示状态和按钮事件
    getWorkflowBtnStatus (pageworkflowConfig) {
      if (pageworkflowConfig.hadWorkflow && pageworkflowConfig.workflowBtnEvents.length) {
        pageworkflowConfig.workflowBtnEvents.forEach(item => {
          let obj = {
            sortNum: item.actionIndex,
            eventsNo: item.eventsNo,
            eventType: item.btnType,
            btnName: item.btnName,
            inputDic: item.inputDic,
            outputDic: item.outputDic
          }
          this.workflowBtnStatus[item.btnType].push(obj)
        })
        // 删除和 保存不需要走流程
        for (let itemKey in this.workflowBtnStatus) {
          if (itemKey !== 'Delete' && itemKey !== 'Save_add' && itemKey !== 'Save_edit' && this.workflowBtnStatus[itemKey].length) {
            let obj = {
              sortNum: 0,
              eventsNo: '',
              eventType: itemKey,
              btnName: this.workflowBtnStatus[itemKey][0].btnName,
              inputDic: [],
              outputDic: []
            }
            this.workflowBtnStatus[itemKey].push(obj)
          }
          this.workflowBtnStatus[itemKey].sort(this.orderByObjAttr('sortNum'))
        }
      } else {
        this.workflowBtnStatus = {
          Delete: [],
          Save_add: [],
          Save_edit: [],
          Submit: [],
          Pass: [],
          Reject: [],
          SelectReject: [],
          Stop: [],
          Apply: [],
          SubmitPending: [],
          ActivePending: []
        }
      }
    },
    // 对象数组排序
    orderByObjAttr (key) {
      return function (val1, val2) {
        return val1[key] - val2[key]
      }
    },
    // 点击流程相关的按钮时
    workflowMethod (type, btnName) {
      let msg = ''
      this.currentClickType = type
      if (type === 'Delete') {
        let info = btnName || '删除'
        msg = '确定要' + info + '该流程表单么?'
        this.$confirm(msg, '提示', {
          confirmButtonText: '确定',
          cancelButtonClass: 'el-button--info',
          cancelButtonText: '取消'
        }).then(() => {
          if (this.workFlowParams.primaryKey) {
            this.loading = true
            this.currentEventsArray = JSON.parse(JSON.stringify(this.workflowBtnStatus[type]))
            if (this.workflowBtnStatus[type][0].sortNum === 0) {
              this.executeWorkflow(1, this.workflowBtnStatus[type][0])
            } else {
              this.getEventsInfoByEventNo(this.models, this.workflowBtnStatus[type][0].eventsNo, null, 1, this.workflowBtnStatus[type][0])
            }
          } else {
            this.$confirm(info + '成功', '提示', {
              showClose: false,
              showCancelButton: false,
              closeOnClickModal: false,
              closeOnPressEscape: false,
              confirmButtonText: '确定'
            }).then(() => {
              this.$refs['formMenuLayout'].resetValidate()
              this.$emit('closeDynamicMenuDialogSuccess')
            }).catch(() => {})
          }
        }).catch(() => {})
      } else {
        if (type === 'Save_add' || type === 'Save_edit') {
          let info = btnName || '保存'
          msg = '确定要' + info + '该流程表单么?'
        } else if (type === 'Submit') {
          let info = btnName || '提交'
          msg = '确定要' + info + '该流程表单么?'
        } else if (type === 'Pass') {
          let info = btnName || '通过'
          msg = '确定要' + info + '该流程表单么?'
        } else if (type === 'Apply') {
          let info = btnName || '申请'
          msg = '确定要' + info + '该流程表单么?'
        } else if (type === 'Reject' || type === 'SelectReject') {
          let info = btnName || '驳回'
          msg = '确定要' + info + '该流程表单么?'
        } else if (type === 'Stop') {
          let info = btnName || '终止'
          msg = '确定要' + info + '该流程表单么?'
        } else if (type === 'SubmitPending') {
          let info = btnName || '提交挂起'
          msg = '确定要' + info + '该流程表单么?'
        } else if (type === 'ActivePending') {
          let info = btnName || '恢复挂起'
          msg = '确定要' + info + '该流程表单么?'
        }
        if (this.$refs['formMenuLayout'].validateFormMethod()) {
          this.$confirm(msg, '提示', {
            confirmButtonText: '确定',
            cancelButtonClass: 'el-button--info',
            cancelButtonText: '取消'
          }).then(() => {
            if (type === 'SelectReject') {
              // 选择驳回时，需要选人
              this.showUserDialog = true
            } else if (type === 'ActivePending') {
              // 恢复挂起时，需要将获取到的人直接带入到NextUserList
              this.getPendingUserList()
            } else {
              this.loading = true
              this.currentEventsArray = JSON.parse(JSON.stringify(this.workflowBtnStatus[type]))
              if (this.workflowBtnStatus[type][0].sortNum === 0) {
                this.executeWorkflow(1, this.workflowBtnStatus[type][0])
              } else {
                this.getEventsInfoByEventNo(this.models, this.workflowBtnStatus[type][0].eventsNo, null, 1, this.workflowBtnStatus[type][0])
              }
            }
          }).catch(() => {})
        }
      }
    },
    // 执行工作流的方法---当与工作相关的事件执行完成之后才执行该方法
    executeWorkflow (workflowEventIndex, workflowEventItem) {
      let submitData = {}
      if (this.clickRow && !this.$isEmptyObject(this.clickRow)) {
        Object.assign(submitData, this.clickRow)
      }
      if (this.models && !this.$isEmptyObject(this.models)) {
        Object.assign(submitData, this.models)
      }
      if (!this.workFlowParams.primaryKey) {
        this.loading = false
        this.$message({
          message: '没有得到与流程相关的PrimaryKey',
          type: 'warning'
        })
        return
      }
      this.$workflowPost('/WorkFlowEngine/Execute', {
        Data: {
          PrimaryKey: this.workFlowParams.primaryKey,
          FormKey: this.dynamicUid,
          NodeID: this.pageFormInfo.pageConfig.relatedWorkflowNode,
          UserID: this.$store.state.userNo,
          UserName: this.$store.state.userName,
          ActionCode: (workflowEventItem.eventType === 'Save_add' || workflowEventItem.eventType === 'Save_edit') ? 'Save' : workflowEventItem.eventType,
          ExtendData: (!this.$isEmptyObject(submitData) && submitData.Extend) ? submitData.Extend : '',
          Remark: (!this.$isEmptyObject(submitData) && submitData.$_Remark) ? submitData.$_Remark : '',
          NextUserList: this.NextUserList
        }
      }).then(data => {
        let dataResult = data.data.result
        if (dataResult.code === 'success') {
          // 获取返回的status值，已给后面的事件使用
          if (dataResult.data && dataResult.data.Status) {
            this.workFlowParams.status = dataResult.data.Status
          }
          // 是最后一个事件时
          if (this.workflowBtnStatus[workflowEventItem.eventType].length === workflowEventIndex) {
            this.loading = false
            this.currentEventsArray = []
            let msg = (workflowEventItem.eventType === 'Save_add' || workflowEventItem.eventType === 'Save_edit') ? '保存成功' : (workflowEventItem.eventType === 'Delete' ? '删除成功' : '操作成功')
            this.$confirm(msg, '提示', {
              showClose: false,
              showCancelButton: false,
              closeOnClickModal: false,
              closeOnPressEscape: false,
              confirmButtonText: '确定'
            }).then(() => {
              // 保存时不需要刷新窗口
              if (workflowEventItem.eventType !== 'Save_add' && workflowEventItem.eventType !== 'Save_edit') {
                if (this.dialogFormVisible) {
                  this.$emit('closeDynamicMenuDialogSuccess')
                } else {
                  this.init()
                }
              }
            }).catch(() => {})
          } else {
            // 还有其它的事件需要执行
            // 需要判断下一个事件是不是执行流程事件
            if (this.workflowBtnStatus[workflowEventItem.eventType][workflowEventIndex].sortNum === 0) {
              this.executeWorkflow(workflowEventIndex + 1, workflowEventItem)
            } else {
              this.getEventsInfoByEventNo(this.models, this.workflowBtnStatus[workflowEventItem.eventType][workflowEventIndex].eventsNo, null, workflowEventIndex + 1, workflowEventItem)
            }
          }
        } else if (dataResult.code === 'failed') {
          this.loading = false
          this.$message({
            message: dataResult.msg,
            type: 'error'
          })
        }
      }).catch(error => {
        this.loading = false
        this.$message({
          message: error,
          type: 'error'
        })
      })
    },
    // 特殊节点需要选人时---目前是actionCode为SelectReject的类型时
    // 放弃选人时---放弃驳回的操作
    giveUpChooseUser () {
      this.showUserDialog = false
    },
    // 确定选人时，需执行下一步的操作
    choosedUsersSuccess (fromChoosedUser) {
      this.showUserDialog = false
      this.NextUserList = fromChoosedUser
      // 选人成功后，执行驳回相关的操作
      this.loading = true
      this.currentEventsArray = JSON.parse(JSON.stringify(this.workflowBtnStatus[this.currentClickType]))
      if (this.workflowBtnStatus[this.currentClickType][0].sortNum === 0) {
        this.executeWorkflow(1, this.workflowBtnStatus[this.currentClickType][0])
      } else {
        this.getEventsInfoByEventNo(this.models, this.workflowBtnStatus[this.currentClickType][0].eventsNo, null, 1, this.workflowBtnStatus[this.currentClickType][0])
      }
    },
    // 恢复挂起时，需要获取NextUserList
    getPendingUserList () {
      this.loading = true
      let submitData = {}
      if (this.clickRow && !this.$isEmptyObject(this.clickRow)) {
        Object.assign(submitData, this.clickRow)
      }
      if (this.models && !this.$isEmptyObject(this.models)) {
        Object.assign(submitData, this.models)
      }
      this.$workflowPost('/WorkFlowEngine/ActivePendingUsers', {
        PrimaryKey: this.workFlowParams.primaryKey,
        NodeID: this.pageFormInfo.pageConfig.relatedWorkflowNode,
        UserID: this.$store.state.userNo,
        ExtendData: (!this.$isEmptyObject(submitData) && submitData.Extend) ? submitData.Extend : ''
      }).then(data => {
        let dataResult = data.data.result
        if (dataResult.code === 'success') {
          // 判断是否有人
          if (dataResult.data.length) {
            this.NextUserList = dataResult.data
            // 执行恢复挂起的相关操作
            this.currentEventsArray = JSON.parse(JSON.stringify(this.workflowBtnStatus[this.currentClickType]))
            if (this.workflowBtnStatus[this.currentClickType][0].sortNum === 0) {
              this.executeWorkflow(1, this.workflowBtnStatus[this.currentClickType][0])
            } else {
              this.getEventsInfoByEventNo(this.models, this.workflowBtnStatus[this.currentClickType][0].eventsNo, null, 1, this.workflowBtnStatus[this.currentClickType][0])
            }
          } else {
            this.loading = false
            // 当没有人时
            this.$message({
              message: '没有获取到恢复挂起的执行人员，请检查接口或流程是否有误',
              type: 'warning'
            })
          }
        } else if (dataResult.code === 'failed') {
          this.loading = false
          this.$message({
            message: dataResult.msg,
            type: 'error'
          })
        }
      }).catch(error => {
        this.loading = false
        this.$message({
          message: error,
          type: 'error'
        })
      })
    }
  }
}
</script>
